<?php

/**
 * 下载密钥
 */
include_once '../Util/Common.php';
require_once __DIR__ . './../Util/Utils.php';

use UtilMethod\Utils;

$utils = new Utils();

$post_data = [
    'orderCode' => 'tb_DownLoadKey',
    'account' => ACCOUNT,
    'password' => PASSWORD,
    'language' => 'PHP'//非必填项,不填默认为Java
];
$datas = base64_encode(json_encode($post_data));
$encrypted = utils::rsaPublicEncrypt($datas, PUBLICKEYPATH);
$params = [//5.4以上版本
    'data' => $encrypted
];
$res = $utils::send_post1(KUBEI, json_encode($params, JSON_UNESCAPED_SLASHES));
$res = json_decode($res, true);
$data = $res['data'];
$count = $res['count'];
$plain_text = mcrypt_decrypt(MCRYPT_3DES, Utils::hexStrToBytes(KEYSTRING, 24), Utils::hexStrToBytes($data), MCRYPT_MODE_ECB);
$resjson = substr($plain_text, 0, $count);
$resArr = json_decode($resjson, true);
$respCode = $resArr['respCode'];
if ("000000" != $respCode) {
    print("下载密钥失败,信息:" . $resArr['respInfo']);
    return;
}
$priKey = $resArr['privatekey'];
if ($priKey == null || $priKey == '') {
    print("下载密钥失败!");
    return;
}
$priKeyFilePath = PRIVATEKEYPATH . $post_data['account'] . "_private_key.pem";
if (file_exists($priKeyFilePath)) {
    unlink($priKeyFilePath);
}
if (!file_put_contents($priKeyFilePath, $resArr['privatekey'])) {
    print("密钥保存文件失败!");
} else {
    print("密钥保存文件成功!路径：" . $priKeyFilePath);
}

 
//$mi="JhLTqOR0+JUIp536nXoX2zmITqHrvXvv4ezZDo0qb6E2LJRBWCs++KboksLxhH86jweXCmrtvW07e7FuS+b+C0LUNEYB40oHuIhtvEcKUF3i9WYvuJSkN3EBGc9aIaWpCBTOzfwazRQ/4Cjw2m1kt4fMtoXed0SK4jDheg3f65g+FcCmpf2f8SP0i0lZrpxW/df2+oo0BbvIM60Fa/0Kz/G9ZmUCLP/gTgWnUv6HAYFaETMLrnRGoK3yp50+8NbsO2uoMt5bNQUa+EXca8TW0HBy6hpWZ01e7GbLH8L7+RTmRNZmPdtPci4fwvgRqorTapsPWWFAzGshTfE1Q65rAg==";
//$path_pri=dirname(__DIR__).'\rsa_private_key.pem';
//$privateKey=openssl_pkey_get_private(file_get_contents($path_pri));
//openssl_private_decrypt(base64_decode($mi),$decrypted,$privateKey);//私钥解密  
//var_dump(base64_decode($decrypted));
//RSA加密
//$publicKey = $utils::redPukey(PUBLICKKEY);
//var_dump($mw);
